<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.pa_id ? '编辑' : '添加'" v-model="visible" align-center
    width="600px" :z-index="2000" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140">
      <el-form-item label="SKU" prop="sku" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
        <el-input v-model="form.sku" placeholder="请输入" class="w-full" />
      </el-form-item>
      <div class="grid grid-cols-6 gap-x-3">
        <el-form-item label="国家" prop="country" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]"
          class="col-span-5">
          <VSelect v-model="form.country" placeholder="请选择" :options="options.country || []" filterable clearable
            class="w-full" />
        </el-form-item>
        <el-form-item label="使用状态">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2" inline-prompt active-text="启用"
            inactive-text="禁用" />
        </el-form-item>
      </div>
      <el-form-item label="海外仓主仓库" prop="warehouse_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]"
        class="col-span-4">
        <VSelect v-model="form.warehouse_id" placeholder="请选择" :options="form.country === 'US' ? options.warehouse_platform_id : options.warehouse_id" filterable clearable class="w-full" />
      </el-form-item>
      <el-form-item label="备注" prop="remark" class="col-span-full">
        <el-input v-model="form.remark" placeholder="备注" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { nextTick, reactive } from 'vue';
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})

// 打开弹窗
const open = async (cw_id) => {
  form.value = {
    country: '',
    warehouse_id: '',
    status: 1,
    remark: '',
  }
  if (!load.value) {
    load.value = true
    await 1
  }
  formRef?.value?.resetFields()
  loadings = reactive({})
  visible.value = true
  // 编辑
  if (cw_id) {
    const res = await api.detail({ cw_id })
    form.value = res.data
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  const validator = await formRef.validate()
  if (!validator) return
  loadings.create = true
  api[form.value.cw_id ? 'edit' : 'create'](form.value).then(res => {
    ElMessage.success('保存成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>